package com.cheyun.netsalev3.viewmodel.aftersale

import android.content.Intent
import android.view.View
import android.widget.DatePicker
import android.widget.TimePicker
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.DaoDianDetailsParam
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.aftersale.CarOwnerData
import com.cheyun.netsalev3.bean.aftersale.MaintenanceDetail
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.repository.aftersale.EditMaintenanceRepository
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.ChoiceOrderCustomerActivity
import com.cheyun.netsalev3.view.aftersale.ChooseCarOwnerActivity
import com.cheyun.netsalev3.view.mars.choosecar.MarsChooseCarActivity
import com.cheyun.netsalev3.viewmodel.BaseViewModel
import com.cheyun.netsalev3.widget.DatePickerFragment
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyDialog
import com.cheyun.netsalev3.widget.TimePickerFragment
import com.jeremyliao.liveeventbus.LiveEventBus
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

class EditMaintenanceActivityViewModel(var data: MaintenanceDetail, var editMaintenanceRepository: EditMaintenanceRepository) : BaseViewModel() {
    var comtime: String = ""
    var activity: AppCompatActivity? = null

    var reservetime = MutableLiveData<String>()
    var sercivename = MutableLiveData<String>()

    var isLoading = false

    fun onClickOk(view: View) {
//        if(isLoading){
//            Toast.makeText(view.context, "正在提交中", Toast.LENGTH_LONG).show()
//            return;
//        }
        if (data.license.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入车牌号", Toast.LENGTH_LONG).show()
            return
        }
        if (data.vin.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入VIN", Toast.LENGTH_LONG).show()
            return
        }
        if (data.seriesname.isNullOrEmpty()) {
            Toast.makeText(view.context, "请选择车系", Toast.LENGTH_LONG).show()
            return
        }
        if (data.specname.isNullOrEmpty()) {
            Toast.makeText(view.context, "请选择车型", Toast.LENGTH_LONG).show()
            return
        }
        if (data.uname.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入客户姓名", Toast.LENGTH_LONG).show()
            return
        }
        if (data.phone.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入客户电话", Toast.LENGTH_LONG).show()
            return
        }
        if (data.reservetime.isNullOrEmpty()) {
            Toast.makeText(view.context, "请选择预约时间", Toast.LENGTH_LONG).show()
            return
        }
        if (data.sercivename.isNullOrEmpty()) {
            Toast.makeText(view.context, "请选择服务顾问", Toast.LENGTH_LONG).show()
            return
        }
        if (data.fee.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入总费用", Toast.LENGTH_LONG).show()
            return
        }
        if (data.mileage.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入行驶里程", Toast.LENGTH_LONG).show()
            return
        }

        var title = ""
        var onSuccess: () -> Unit = {
            isLoading = false
            MyToast(title)
            LiveEventBus.get().with("aftersale_maintenance_refresh").postValue("")
            LiveEventBus.get().with("aftersale_maintenance_detail_refresh").postValue("")
            myFinish()
        }
        isLoading = true
        if (data.id == 0) {
            title = "添加成功"
            editMaintenanceRepository.updateData(data, onSuccess)
        } else {
            title = "更新成功"
            editMaintenanceRepository.updateData(data, onSuccess)
        }
    }

    var dataShow = ObservableField<MaintenanceDetail>(data)

    fun onClickCancel(view: View) {
        myFinish()
    }

    lateinit var content: AppCompatActivity

    /**
     * 离店
     */
    fun goTime(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            this.content = content
            var c = Calendar.getInstance()
            c.add(Calendar.DAY_OF_MONTH,13)
            var datePickerFragment = DatePickerFragment()
            datePickerFragment.minDate = Calendar.getInstance().timeInMillis
            datePickerFragment.maxDate =  c.timeInMillis
            datePickerFragment.clickOk = this::goDateOk

            datePickerFragment.show(content.supportFragmentManager, "")
        }
    }

    var year1 = 0
    var month1 = 0
    var day1 = 0
    /**
     * 离店日期回调
     */
    fun goDateOk(view: DatePicker, year: Int, month: Int, day: Int) {
        this.year1 = year
        this.month1 = month
        this.day1 = day
        var timePickerFragment = TimePickerFragment()
        timePickerFragment.clickOk = this::goTimeOk
        timePickerFragment.show(content.supportFragmentManager, "")
    }

    /**
     * 离店时间回调
     */
    fun goTimeOk(view: TimePicker, hourOfDay: Int, minute: Int) {
        comtime = FunctionUtils.formatTime(year1.toString(), month1.toString(), day1.toString(), hourOfDay, minute)
        data.reservetime = comtime

        reservetime.postValue(comtime)
    }

    var memberlist = ArrayList<Member>()

    init {
//        MySharedPreferences.getBaseData()?.member?.apply {
//            memberlist.addAll(this)
//        }
        getMemberlist()
    }

    fun getMemberlist(){
        editMaintenanceRepository.getAftersaleUsableUserList(this::getMemberBack)
    }
    fun getMemberBack(ms:List<Member>){
        memberlist.addAll(ms)
    }

    /**
     * 点击接待人员
     */
    fun onclickRcvname(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            var list = ArrayList<DialogParam>()
            for (item in memberlist) {
                if(item.isadmin == 0) {
                    var dialogParam = DialogParam(item.realname, item.uid.toString())
                    list.add(dialogParam)
                }
            }
            var myDialog = MyDialog.newInstance("请选择服务顾问", list)
            myDialog.huidia = this::rcvName
            myDialog.show(content.supportFragmentManager, "")
        }
    }

    /**
     * 接待人回调
     */
    fun rcvName(item: DialogParam?) {
        item?.apply {
            data.serciveuid = item.id
            data.sercivename = item.name


            sercivename.postValue(item.name)
        }
    }

    fun onClickSearchCepai(view: View){
        if (data.license.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入车牌号", Toast.LENGTH_LONG).show()
            return
        }
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(content, ChooseCarOwnerActivity::class.java)
            intent.putExtra("source","maintenance")
            intent.putExtra("nkey","local")
            intent.putExtra("license",data.license)
            content.startActivityForResult(intent, 101)
        }
    }

    fun onClickSearchVin(view: View){
        if (data.engineno.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入发动机号", Toast.LENGTH_LONG).show()
            return
        }
        if (data.vin.isNullOrEmpty()) {
            Toast.makeText(view.context, "请输入VIN", Toast.LENGTH_LONG).show()
            return
        }
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(content, ChooseCarOwnerActivity::class.java)
            intent.putExtra("source","maintenance")
            intent.putExtra("nkey","all")
            intent.putExtra("engineno",data.engineno)
            intent.putExtra("vin",data.vin)
            content.startActivityForResult(intent, 101)
        }
    }

    /**
     * 品系
     */
    fun onClickBrandname(view: View) {
        if(data.reservetype.equals("1")) return
        var content = view.context
        if (content is AppCompatActivity) {
            var carBrandParam = CarBrandParam()
            var intent = Intent(content, MarsChooseCarActivity::class.java)
            intent.putExtra("car", carBrandParam)
            intent.putExtra("notop", true)
            intent.putExtra("Isspec", true)
            intent.putExtra("enableClickAll",true)
            content.startActivityForResult(intent, 102)
        }
    }

    /**
     * 品牌车系回调
     */
    fun setCarData(carBrandParam: CarBrandParam) {
        carBrandParam?.apply {
            data.seriesname = carBrandParam.seriesname
            data.specname = carBrandParam.specname
            data.brandgroup = carBrandParam.series + "," + carBrandParam.spec
        }
    }

    fun setCarOwner(carOwnerData:CarOwnerData){
        data.uname = carOwnerData.name
        data.phone = carOwnerData.phone
        data.vin = carOwnerData.vin
        data.engineno = carOwnerData.engineno
        data.license = carOwnerData.license
        data.series = carOwnerData.seriesid.toInt()
        data.seriesname = carOwnerData.seriesname
        data.spec = carOwnerData.specid.toInt()
        data.specname = carOwnerData.specname
        data.mileage = carOwnerData.mileage

        data.brandgroup = carOwnerData.seriesid + "," + carOwnerData.specid

        data.engineno = carOwnerData.engineno

        dataShow.notifyChange()
    }



}